﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Bdcs.Application.Mappers.Dto;
using Bdcs.Core.Entity;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;

namespace Bdcs.Application.Services
{
    /// <summary>
    /// 围栏管理接口
    /// </summary>
    [ApiDescriptionSettings("围栏管理接口@202")]
    public class RouteAppService : IDynamicApiController
    {
        
        private readonly IRepository<CRRouteInfo_MMod> _routeRepository;
        private readonly IRepository<SYS_CompanyInfoMod> _companyRepository;
        /// <summary>
        /// 构造方法注入
        /// </summary>
        /// <param name="routeRepository"></param>
        public RouteAppService(IRepository<CRRouteInfo_MMod> routeRepository,IRepository<SYS_CompanyInfoMod> companyRepository)
        {
            _routeRepository = routeRepository;
            _companyRepository = companyRepository;
        }
        
        /// <summary>
        /// 围栏分页查询
        /// </summary>
        /// <returns></returns>
        public PagedList<RouteDto> List(int pageIndex, int pageSize, string userId ,[FromQuery] string RouteName = "") {
            var query = @"Select RouteID ,RouteName ,ISNULL(B.CompanyName,'') AS CompanyName 
            From dbo.CRRouteInfo_M A Left Join Sys_CompanyInfo B 
            ON A.CompanyID=B.CompanyID 
            where RouteType=1 AND (RouteName Like '%' + @RouteName + '%') AND A.CompanyID IN (Select CompanyID From Adv_UserDept Where UserID=@userId)".SqlQuery<RouteDto>(new
            {
                RouteName = RouteName,
                userId = userId
            });
            var pageList = new PagedList<RouteDto>();
            int count = query.Count<RouteDto>();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            pageList.PageIndex = pageIndex;
            pageList.PageSize = pageSize;
            pageList.TotalCount = count;
            pageList.Items = list;
            if (count % pageSize == 0)
            {
                pageList.TotalPages = count / pageSize;
            }
            else
            {
                pageList.TotalPages = Convert.ToInt32(count / pageSize) + 1;
            }

            return pageList;
        }


        /// <summary>
        /// 新增围栏数据,opmode=1时为update
        /// </summary>
        /// <param name="routeVo">接收对象</param>
        /// <returns>受影响行数</returns>
        public int Add(RouteInput routeInput)
        {
            var rowEffects =
                @"exec usp_Web_RouteInfo_save @RouteId,@RouteName,@CompanyID,@RouteWidth,@RouteType,@Points,@Opmode"
                    .SqlNonQuery(new
                    {
                        RouteId = routeInput.RouteID,
                        RouteName = routeInput.RouteName,
                        CompanyID = routeInput.CompanyID,
                        RouteWidth = routeInput.RouteWidth,
                        RouteType = routeInput.RouteType,
                        Points = routeInput.Points,
                        Opmode = routeInput.Opmode
                    });
            Console.WriteLine(rowEffects);
            return rowEffects;
        }

        /// <summary>
        /// 删除围栏
        /// </summary>
        /// <param name="routeId">围栏编号</param>
        /// <returns></returns>
        public int Delete(string routeId)
        {
            var rowEffects = @"exec usp_Web_RouteInfo_Delete @routeId,@userId ".SqlNonQuery(new
            {
                routeId = routeId,
                userId = "" // 其实可以不传,该参数在存储过程中没有意义
            });
            return rowEffects;
        }
        
    }
}